﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.OleDb;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml;
using System.Xml.Linq;

public partial class ajax_ItemManage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string action = Request.QueryString["action"];
        if (null != action)
        {
            action = action.ToString().ToLower();
            switch (action)
            {
                case "getmajor" : GetMajor(); 
                    break;
                case "insert" : InsertItem();
                    break;
                case "delete": DeleteItem();
                    break;
                case "modify": ModifyItem();
                    break;
                case "update": UpdateItem();
                    break;

            }

        }

    }


    private string ChapterNo;
    private string ItemNo;
    private string Item;
    private string Major;
    private string FaultResult;
    private string Suggestion;
    private string SpecialSuggestion;
    private string EncodeName;

    protected void GetMajor()
    {
        string major = "";
        DBOperate MyBb = new DBOperate();
        MyBb.SetCommandString("SELECT Major FROM MajorList;");
        OleDbDataReader reader = MyBb.ExecuteReader();
        while (reader.Read()) 
        {
            major += "|";
            major += reader["Major"];
        }
        MyBb.CloseDB();
        Response.Write(major);
    }

    protected void InsertItem()
    {
        RequestPara();
        string[] SugArray = Suggestion.Split('|');        
        DBOperate MyDb = new DBOperate();
        MyDb.SetCommandString(string.Format("INSERT INTO DataList(ChapterNo,Item,Major,Fault_Result,Engineer,SkilledPeple,QC,Measures) VALUES ({0},'{1}','{2}','{3}',{4},{5},{6},'{7}')", ChapterNo, Item, Major, FaultResult, InArray(SugArray, "engineer"), InArray(SugArray, "skilledPeople"), InArray(SugArray, "qc"), SpecialSuggestion));
        //string InsertStr = "insert into DataList values(21,23,'dd','ME','错误',true,true,true,'无',0)";
        //MyDb.SetCommandString(InsertStr);
        MyDb.ExecuteNonReader();
        MyDb.CloseDB();
        Response.Write(EncodeName);

    }

    protected void UpdateItem()
    {
        RequestPara();
        string[] SugArray = Suggestion.Split('|');
        DBOperate MyDb = new DBOperate();//(ChapterNo,Item,Major,Fault_Result,Engineer,SkilledPeple,QC,Measures)
        MyDb.SetCommandString(string.Format("UPDATE DataList SET [ChapterNo]={0}, [Item]='{1}', [Major]='{2}', [Fault_Result]='{3}', [Engineer]={4}, [SkilledPeple]={5}, [QC]={6}, [Measures]='{7}' WHERE [No]={8}", ChapterNo, Item, Major, FaultResult, InArray(SugArray, "engineer"), InArray(SugArray, "skilledPeople"), InArray(SugArray, "qc"), SpecialSuggestion, ItemNo));
        MyDb.ExecuteNonReader();
        MyDb.CloseDB();
    }

    protected void ModifyItem()
    {
        int itemNo = Convert.ToInt32(Request.QueryString["itemNo"]);
        DBOperate MyDb = new DBOperate();
        MyDb.SetCommandString(string.Format("SELECT * FROM DataList WHERE [No] = {0}", itemNo));
        OleDbDataReader reader = MyDb.ExecuteReader();
        if (reader.HasRows && reader.Read())
        {
            XMLInit();
            _e("<ItemOuter>");
            OutputXML("ChapterNo", reader["ChapterNo"].ToString());
            OutputXML("ItemNo", reader["No"].ToString());
            OutputXML("Item", reader["Item"].ToString());
            OutputXML("Major", reader["Major"].ToString());
            OutputXML("Fault_Result", reader["Fault_Result"].ToString());
            OutputXML("Engineer", reader["Engineer"].ToString().ToLower());
            OutputXML("SkilledPeople", reader["SkilledPeple"].ToString().ToLower());
            OutputXML("QC", reader["QC"].ToString().ToLower());
            OutputXML("Measures", reader["Measures"].ToString());
            _e("</ItemOuter>");
            Response.End();           
        }
        MyDb.CloseDB();

        


    }

    protected void OutputXML(string tagName, string text) 
    {
        string xml = "<";
        xml += tagName;
        xml += ">";
        xml += text;
        xml += "</";
        xml += tagName;
        xml += ">";
        Response.Write(@xml);
    }

    protected void OutputXML(string tagName)
    {

    }

    protected void _e(string str)
    {
        Response.Write(@str);
    }

    protected void XMLInit() 
    {
        Response.ContentType = "application/xml";
        Response.Charset = "utf-8";
        Response.Write("<?xml version='1.0' encoding='utf-8'?>");
    }

    protected void DeleteItem()
    {
        int itemNo = Convert.ToInt32(Request.QueryString["itemNo"]);
        DBOperate MyDb = new DBOperate();
		//MyDb.SetCommandString(string.Format("DELETE FROM DataList WHERE [No] = {0}",3));
        MyDb.SetCommandString(string.Format("DELETE FROM DataList WHERE [No] = {0}",itemNo));
        //MyDb.SetCommandString("DELETE FROM DataList WHERE ChapterNo = 21");
        MyDb.ExecuteNonReader();
        MyDb.CloseDB();
        Response.Write(itemNo);
    }

    protected void RequestPara()
    {
        ChapterNo = Request.QueryString["ChapterNo"];
        ItemNo = Request.QueryString["itemNo"];
        Item = Request.QueryString["item"];
        Major = Request.QueryString["major"];
        FaultResult = Request.QueryString["faultResult"];
        Suggestion = Request.QueryString["suggestion"];
        SpecialSuggestion = Request.QueryString["specialSuggestion"];

        //以下暂时仅供测试
        //SpecialSuggestion = Server.UrlEncode(SpecialSuggestion);
        //SpecialSuggestion = SpecialSuggestion.Replace("%0a", "%3cbr+%2f%3e");
        //SpecialSuggestion = Server.UrlDecode(SpecialSuggestion);

    }

    //判断某字符串是否存在于数组中
    public string InArray(string[] Array, string Str)
    {
        string Result = "false";
        for (int i = 0; i < Array.Length; i++)
        {
            if (Str == Array[i])
            {
                Result = "true";
            }
        }
        return Result;
    }
}
